#ifndef __RENDER_MESH_H__
#define __RENDER_MESH_H__

#pragma once

#include "RenderObject.h"
#include "BoundingVolume.h"

namespace Nezha
{

	class SceneNode;

	/** single mesh object. */
	class NEZHA_ENGINE_ENTRY RenderMesh : public RenderObject, public Refable
	{
	public:
		virtual ~RenderMesh();

		/** for render pipeline. */
		virtual void prepareGeometry4Pipeline(GeometryDataBatch& batch);

		virtual u32 getGeometryHash() const;

		virtual SceneNode* getSpatial()
		{
			return mSceneNode;
		}

		virtual const _string& getROName() const;

		// TODO bounds?

		/**
		@remarks
			this will cause the buffers to be locked, use endAccess() to release the lock after you finish your operation.
		*/
		VertexBufferAccessor* beginAccess();

		/**
		@remarks
			
		*/
		void endAccess();

		const AABB& getBounding() const
		{
			return mBounding;
		}

		const GeometryData* getDataRaw() const
		{
			return mGeometryDataPtr.ptr();
		}

		RenderMesh(GeometryData* gd);

		virtual const AABB& recaculateBounding();

NEZHA_INTERNAL:
		//GeometryData* _getDataRaw()
		//{
		//	return mGeometryDataPtr.get();
		//}

		/** for initializing or special purpose. */
		void _setAABB(const AABB& aabb);

		/** set parent node, should only be dispatched by mesh creator. */
		void _setSceneHierarchy(SceneNode* sn);
	protected:
		virtual void updateNormals() = 0;
		virtual void updateTangents(u32 tangentSpace) = 0;

		GeometryDataPtr mGeometryDataPtr;
		VertexBufferAccessor* mVBAccessor;
		SceneNode* mSceneNode;

		AABB mBounding;
	};

	typedef RefCountedPtr<RenderMesh>::Default RenderMeshPtr;

}//end namespace Nezha

#endif //end __RENDER_MESH_H__